package com.ylbh.modules.banner.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.banner.dto.BannerListDTO;
import com.ylbh.common.pojo.banner.dto.StartAdvertisingDTO;
import com.ylbh.common.pojo.goods.dto.GoodsClassDTO;
import com.ylbh.common.utils.*;
import com.ylbh.modules.banner.entity.AppHomeBanner;
import com.ylbh.modules.banner.service.AppHomeBannerService;
import com.ylbh.modules.userclass.service.ShoppingUserClassService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * appBanner管理
 * @author czx
 * @date 2019-05-08 18:04:15
 */
@RestController
@RequestMapping("banner")
public class AppHomeBannerController {

    @Autowired
    private AppHomeBannerService appHomeBannerService;

    @Autowired
    private ShoppingUserClassService shoppingUserClassService;

    @Value("${url.iconFileName}")
    private String iconFileName;

    @ApiOperation("banner列表")
    @RequestMapping("queryBannerList")
    @RequiresPermissions("banner:queryBannerList")
    public CommonResult<PageUtils> queryBannerList(BannerListDTO bannerListDTO) {
        PageUtils list = appHomeBannerService.queryBannerList(bannerListDTO);
        return Ref.ok(list);
    }

    @ApiOperation("banner详情")
    @RequestMapping("getBannerById/{id}")
    @RequiresPermissions("banner:getBannerById")
    public CommonResult<AppHomeBanner> getBannerById(@PathVariable(value = "id")Long id) {
        AppHomeBanner homeBanner = appHomeBannerService.getBannerById(id);
        return Ref.ok(homeBanner);
    }

    @SysLog("修改banner")
    @ApiOperation("修改banner")
    @RequestMapping("modifyBannerById")
    @RequiresPermissions("banner:modifyBannerById")
    public CommonResult<Void> modifyBannerById(@RequestBody AppHomeBanner appHomeBanner) {
        appHomeBannerService.modifyBannerById(appHomeBanner);
        return Ref.ok();
    }

    @SysLog("新增banner")
    @ApiOperation("新增banner")
    @RequestMapping("addBanner")
    @RequiresPermissions("banner:addBanner")
    public CommonResult<Void> addBanner(@RequestBody AppHomeBanner appHomeBanner) {
        appHomeBannerService.addBanner(appHomeBanner);
        return Ref.ok();
    }

    @SysLog("删除banner")
    @ApiOperation("删除banner")
    @RequestMapping("removeBanner")
    @RequiresPermissions("banner:removeBanner")
    public R delete(@RequestBody Long[] ids){
		appHomeBannerService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @ApiOperation("上传Banner图片")
    @RequestMapping("uploadBanner")
    public CommonResult<String> uploadBanner(MultipartFile file){
        String url = appHomeBannerService.uploadBanner(file);
        return Ref.ok(url);
    }

    @ApiOperation("根据分辨率条件来上传图片")
    @RequestMapping("uploadAdvertisingMap")
    public CommonResult uploadAdvertisingMap(MultipartFile file, HttpServletRequest request){
        String type = request.getParameter("type");
        String url = appHomeBannerService.uploadAdvertisingMap(file, type);
        return Ref.ok(url);
    }

    @SysLog("修改Banner图片显示状态")
    @ApiOperation("修改Banner图片显示状态")
    @RequestMapping("uploadBanner/{id}/{showStatus}")
    public CommonResult<Void> modifyShowStatus(@PathVariable Long id,@PathVariable Integer showStatus) {
        appHomeBannerService.modifyShowStatus(id, showStatus);
        return Ref.ok();
    }

    @SysLog("修改icon信息")
    @ApiOperation("修改icon信息")
    @RequestMapping("modifyIconDetails")
    public CommonResult<Void> modifyIconDetails(@RequestBody AppHomeBanner appHomeBanner) {
        appHomeBanner.setImgPath("icon");
        appHomeBanner.setAddTime(new Date());
        appHomeBannerService.updateById(appHomeBanner);
        return Ref.ok();
    }

    @SysLog("新增icon信息")
    @ApiOperation("新增icon信息")
    @RequestMapping("addIconDetails")
    public CommonResult<Void> addIconDetails(@RequestBody AppHomeBanner appHomeBanner) {
        appHomeBanner.setImgPath("icon");
        appHomeBanner.setBannerType("5");
        appHomeBanner.setShowStatus(1);
        appHomeBanner.setAddTime(new Date());
        AppHomeBanner banner1 = new AppHomeBanner();
        BeanUtils.copyProperties(appHomeBanner, banner1);
        if (appHomeBanner.getAppType() == 0) {
            for (int i = 0; i < 3; i++) {
                banner1.setAppType(i + 1);
                appHomeBannerService.save(banner1);
            }
        } else {
            banner1.setAppType(null);
            appHomeBannerService.save(banner1);
        }
        return Ref.ok();
    }

    @SysLog("新增启动广告")
    @ApiOperation("新增启动广告")
    @PostMapping("addStartAdvertising")
    @RequiresPermissions("banner:addStartAdvertising")
    public CommonResult addStartAdvertising(@RequestBody StartAdvertisingDTO startAdvertisingDTO) {
        appHomeBannerService.addStartAdvertising(startAdvertisingDTO);
        return Ref.ok();
    }

    @SysLog("修改启动广告")
    @ApiOperation("修改启动广告")
    @PostMapping("modifyStartAdvertisingById")
    @RequiresPermissions("banner:modifyStartAdvertisingById")
    public CommonResult modifyStartAdvertisingById(@RequestBody StartAdvertisingDTO startAdvertisingDTO) {
        appHomeBannerService.modifyStartAdvertisingById(startAdvertisingDTO);
        return Ref.ok();
    }

    @SysLog("启动广告详情")
    @ApiOperation("启动广告详情")
    @GetMapping("getStartAdvertisingById/{id}")
    @RequiresPermissions("banner:getStartAdvertisingById")
    public CommonResult getStartAdvertisingById(@PathVariable Long id) {
        StartAdvertisingDTO dto = appHomeBannerService.getStartAdvertisingById(id);
        return Ref.ok(dto);
    }

    @ApiOperation("启动广告页列表")
    @GetMapping("queryStartAdvertisingList")
    @RequiresPermissions("banner:queryStartAdvertisingList")
    public CommonResult queryStartAdvertisingList(PageDTO pageDTO) {
        PageUtils page = appHomeBannerService.queryStartAdvertisingList(pageDTO);
        return Ref.ok(page);
    }

    @ApiOperation("首页底部菜单icon")
    @GetMapping("queryBottomIconList")
//    @RequiresPermissions("banner:queryBottomIconList")
    public CommonResult queryBottomIconList(PageDTO pageDTO) {
        PageUtils pageUtils = appHomeBannerService.queryBottomIconList(pageDTO);
        return Ref.ok(pageUtils);
    }

    @ApiOperation("首页底部菜单icon图标上传")
    @RequestMapping("uploadBottomIcon")
    public CommonResult uploadBottomIcon(MultipartFile file, HttpServletRequest request) {
        String url = appHomeBannerService.uploadBottomIcon(file, request);
        if ("上传图片大小不能超过1M！".equals(url))
            return Ref.error("上传图片大小不能超过1M");
        else
            return Ref.ok(url);
    }

    @SysLog("新增首页底部菜单icon")
    @ApiOperation("新增首页底部菜单icon")
    @PostMapping("addBottomIcon")
    public CommonResult addBottomIcon(@RequestBody AppHomeBanner appHomeBanner) {
        appHomeBanner.setAddTime(new Date());
        appHomeBanner.setImgPath(iconFileName);
        appHomeBannerService.save(appHomeBanner);
        return Ref.ok();
    }

    @SysLog("修改首页底部菜单icon")
    @ApiOperation("修改首页底部菜单icon")
    @PostMapping("modifyBottomIcon")
    public CommonResult modifyBottomIcon(@RequestBody AppHomeBanner appHomeBanner) {
        appHomeBannerService.updateById(appHomeBanner);
        return Ref.ok();
    }

    @ApiOperation("图片跳转分类")
    @GetMapping("/queryOuterClassList")
    public CommonResult<List<GoodsClassDTO>> queryOuterClassList(){
        List<GoodsClassDTO> goodsClasses = appHomeBannerService.queryOuterClassList();
        return Ref.ok(goodsClasses);
    }
}
